#include "MultiObjEnvRenderer.hpp"
#include "../Graphics\ColorDefine.hpp"
#include <Utility/StringPrintf.hpp>

namespace zzz {
void MultiObjEnvRenderer::DrawObj()
{
  ColorDefine::white.ApplyGL();
  glBegin(GL_QUADS);
  glVertex3d(-1,-1,0);
  glVertex3d(1,-1,0);
  glVertex3d(1,1,0);
  glVertex3d(-1,1,0);
  glEnd();
}

void MultiObjEnvRenderer::SetupCamera()
{
  camera_.ApplyGL();
  objArcBall_[curObjArcBall_].ApplyGL();
}


MultiObjEnvRenderer::MultiObjEnvRenderer()
{
  allArcBall_=false;
  lockArcBall_=false;
  curObjArcBall_=0;
  pCurObjArcBall_=&objArcBall_[0];
  pCurEnvArcBall_=&envArcBall_;
}

void MultiObjEnvRenderer::OnLButtonDown(unsigned int nFlags,int x,int y)
{
  if (!lockArcBall_) {
    if (allArcBall_) {
      for (int i=0; i<10; i++) {
        objArcBall_[i].OnLButtonDown(nFlags, x, y);
      }
    } else {
      objArcBall_[curObjArcBall_].OnLButtonDown(nFlags, x, y);
    }
    envArcBall_.OnLButtonDown(nFlags, x, y);
  }
}

void MultiObjEnvRenderer::OnLButtonUp(unsigned int nFlags,int x,int y)
{
  for (int i=0; i<10; i++)
    objArcBall_[i].OnLButtonUp(nFlags, x, y);
  envArcBall_.OnLButtonUp(nFlags, x, y);
}

void MultiObjEnvRenderer::OnRButtonDown(unsigned int nFlags,int x,int y)
{
  if (!lockArcBall_) {
    envArcBall_.OnLButtonDown(nFlags, x, y);
  }
}

void MultiObjEnvRenderer::OnRButtonUp(unsigned int nFlags,int x,int y)
{
  envArcBall_.OnLButtonUp(nFlags, x, y);
}

void MultiObjEnvRenderer::OnMButtonDown(unsigned int nFlags,int x,int y)
{
  if (!lockArcBall_) {
    if (allArcBall_) {
      for (int i=0; i<10; i++) {
        objArcBall_[i].OnLButtonDown(nFlags, x, y);
      }
    } else {
      objArcBall_[curObjArcBall_].OnLButtonDown(nFlags, x, y);
    }
  }
}

void MultiObjEnvRenderer::OnMButtonUp(unsigned int nFlags,int x,int y)
{
  for (int i=0; i<10; i++)
    objArcBall_[i].OnLButtonUp(nFlags, x, y);
}

void MultiObjEnvRenderer::OnMouseMove(unsigned int nFlags,int x,int y)
{
  if (!lockArcBall_) {
    if (allArcBall_) {
      for (int i=0; i<10; i++)
        objArcBall_[i].OnMouseMove(nFlags, x, y);
    } else
      objArcBall_[curObjArcBall_].OnMouseMove(nFlags, x, y);
    envArcBall_.OnMouseMove(nFlags, x, y);
  }
  Redraw();
}

void MultiObjEnvRenderer::OnChar(unsigned int nChar, unsigned int nRepCnt, unsigned int nFlags)
{
  switch(nChar) {
  case ZZZKEY_NUM_DOT:  //'numpad .'
    allArcBall_=true;
    printf("All the ArcBalls are controled\n");
    Redraw();
    break;
  case ZZZKEY_NUM_0:
    curObjArcBall_=0;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_1:
    curObjArcBall_=1;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_2:
    curObjArcBall_=2;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_3:
    curObjArcBall_=3;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_4:
    curObjArcBall_=4;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_5:
    curObjArcBall_=5;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_6:
    curObjArcBall_=6;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_7:
    curObjArcBall_=7;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_8:
    curObjArcBall_=8;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
    break;
  case ZZZKEY_NUM_9:
    curObjArcBall_=9;allArcBall_=false;pCurObjArcBall_=&(objArcBall_[curObjArcBall_]);
    printf("Current ArcBall: %d\n",curObjArcBall_);
    Redraw();
  }
}

void MultiObjEnvRenderer::AfterOnSize(unsigned int nType, int cx, int cy)
{
  for (int i=0; i<10; i++)
    objArcBall_[i].OnSize(nType, cx, cy);
  envArcBall_.OnSize(nType, cx, cy);
}

void MultiObjEnvRenderer::ResetArcball()
{
  envArcBall_.Reset();
  for (zuint i=0; i<10; i++)
    objArcBall_[i].Reset();
}

void MultiObjEnvRenderer::CreateMsg()
{
  SStringPrintf(msg_,"Current ArcBall: %d, Res: %d x %d, SPF: %lf = FPS: %lf",allArcBall_?123:curObjArcBall_,width_,height_,SPF_,FPS_);
}

bool MultiObjEnvRenderer::InitData()
{
  envArcBall_.Init(this);
  for (zuint i=0; i<10; i++)
    objArcBall_[i].Init(this);
  return ArcBallRenderer::InitData();
}


}